package com.neo.controller.admin;

import cn.shuibo.annotation.Decrypt;
import cn.shuibo.annotation.Encrypt;
import com.neo.bean.UserManagement;
import com.neo.common.base.AppConstants;
import com.neo.common.utils.JuHeUtils;
import com.neo.common.utils.StringUtils;
import com.neo.common.utils.jedis.JedisClient;
import com.neo.controller.CommonController;
import com.neo.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserInfoController extends CommonController {
    @Autowired
    private UserService userService;

    @Decrypt
    @Encrypt
    @PostMapping(value = "/login")
    @ApiOperation(value = "用户登录", notes = "/login", response = UserManagement.class)
    @ResponseBody
    public Map<String, Object> login(@ApiParam("登录账号") @RequestParam(value = "accountNumber") String accountNumber,
                                     @ApiParam("密码") @RequestParam(value = "passWord") String passWord) {

        //登录
        UserManagement userManagement = userService.login(accountNumber);
        if (userManagement == null) {
            return fail(AppConstants.USER_LOGIN_ERR, "账号不存在");
        } else {
            userManagement = userService.login(accountNumber, passWord);
            if (userManagement == null) {
                return fail("账号或密码错误");
            }
            return sucess(initLoginAdminMamgerInfo(userManagement));
        }
    }

    @PostMapping(value = "/updatePassWord")
    @ApiOperation(value = "修改密码", notes = "/updatePassWord")
    @ResponseBody
    public Map<String, Object> updatePassWord(@ApiParam("手机号") @RequestParam(value = "tellPhone") String tellPhone,
                                              @ApiParam("验证码") @RequestParam(value = "code") String code,
                                              @ApiParam("密码") @RequestParam(value = "passWord") String passWord) {
        // 获取redis 缓存的验证码
        String randomNum = jedisClient.get(tellPhone);
        if (StringUtils.isNullOrEmpty(randomNum)) {
            return fail("验证码已过期");
        }
        if (!randomNum.equals(code)) {
            return fail("验证码输入错误");
        }
        //登录
        UserManagement userManagement = userService.selectByTellPhone(tellPhone);
        if (userManagement == null) {
            return fail("账号不存在");
        }
        userManagement.setPassWord(passWord);
        int i = userService.updatePassWord(userManagement);
        if (i > 0) {
            jedisClient.del(tellPhone);
            return sucess();
        }
        return fail();
    }

    @PostMapping(value = "/sendCode")
    @ApiOperation(value = "发送验证码", notes = "/sendCode")
    @ResponseBody
    public Map<String, Object> login(@ApiParam("手机号码") @RequestParam(value = "tellPhone") String tellPhone) {
        //登录
        UserManagement userManagement = userService.selectByTellPhone(tellPhone);
        if (userManagement == null) {
            return fail("手机号未注册管理系统,请修改后重试");
        }
        String code = JuHeUtils.generateCode();
        // 验证码存入redis
        jedisClient.set(tellPhone, code);
        jedisClient.expire(tellPhone, 300);
        return sucess();
    }
}
